﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_selectproduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnOk.Click += new EventHandler(btnOk_Click);
        this.ddlProductClass.SelectedIndexChanged += new EventHandler(ddlProductClass_SelectedIndexChanged);
        if (!IsPostBack)
        {
            this.ddlProductClass.Items.Clear();
            this.ddlProductClass.Items.Add(new ListItem("请选择产品系列", ""));
            InitCategory(0, "");            
        }        
    }

    void ddlProductClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.ddlProduct.Items.Clear();
        this.ddlProduct.Items.Add(new ListItem("请选择产品", ""));
        CProduct p = new CProduct();
        DataTable dt = p.ReadProducts(Convert.ToInt32(this.ddlProductClass.SelectedValue));
        if (null != dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlProduct.Items.Add(new ListItem(dt.Rows[i]["title"].ToString(), dt.Rows[i]["id"].ToString()));
            }
        }
        this.up.Update();
    }

    private void InitCategory(int id, string split)
    {
        CProduct p = new CProduct();
        DataTable dt = p.ReadCategories(id);
        if (null != dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlProductClass.Items.Add(new ListItem(split + dt.Rows[i]["title"].ToString(), dt.Rows[i]["id"].ToString()));
                InitCategory(Convert.ToInt32(dt.Rows[i]["id"].ToString()), split + "--");
            }
        }
    }

    void btnOk_Click(object sender, EventArgs e)
    {
        if (this.ddlProduct.SelectedIndex == 0 || this.ddlProductClass.SelectedIndex == 0)
        {
            ScriptManager.RegisterClientScriptBlock(Page, up.GetType(), "", "alert('请选择产品系列和产品');", true);
            return;
        }
        CIndex index = new CIndex();
        string id = Request.QueryString["id"].ToString();
        index.UpdateProduct(Convert.ToInt32(id), Convert.ToInt32(this.ddlProduct.SelectedValue));
        ScriptManager.RegisterClientScriptBlock(Page, up.GetType(), "", "alert('操作完成!');", true);
        //Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "<script>alert('操作成功!');this.close();window.opener.location.reload();</script>");
    }
}